package paper.web.controller;

import java.util.List;
import java.util.Map;

import keter.web.util.MediaTypes;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import paper.domain.Message;
import paper.service.MessageService;

@Controller
@RequestMapping("/paper/message")
public class MessageController extends BaseController {
		
	@Autowired
	MessageService mservice;

	private static final Logger logger = LoggerFactory.getLogger(MessageController.class);

	public MessageController() {
	}
	
	public static class Views {
		private static final String Prefix = "paper/message";
		static final String Index = to_(Prefix) + "index";
		static final String View = to_(Prefix) + "view";
		static final String Add = to_(Prefix) + "add";
		static final String Delete = to_(Prefix) + "delete";
	}

	/**
	 * <p>Method ：index
	 * <p>Description : 进入消息页面
	 *
	 * @param model
	 * @return 
	 * @author  顾力行-gulixing@msn.com
	 */
	@RequestMapping(value = Routes.Index, method = RequestMethod.GET)
	public String index(Model model) {
		return Views.Index;
	}
	
	/**
	 * <p>
	 * Method ：getMyTopic
	 * <p>
	 * Description : 获取列表数据
	 * 
	 * @return
	 * @throws Exception
	 * @author 顾力行-gulixing@msn.com
	 */
	@RequestMapping(value = Routes.GetAll, method = RequestMethod.GET)
	@ResponseBody
	public List<Message> getAll() throws Exception {
		return mservice.getDao().findInboxByReceiverId(getCurrentUser().getId());
	}
	
	/**
	 * <p>Method ：view
	 * <p>Description : 进入查看页面
	 * @param id
	 * @param model
	 * @return 
	 * @author  gulixing@msn.com
	 * @version 1.0.0
	 */
	@RequestMapping(value = Routes.View, method = RequestMethod.GET)
	public String view(@PathVariable Long id, Model model) {
		model.addAttribute("message", mservice.getDao().findOne(id));
		return Views.View;
	}
	
	
	@RequestMapping(value = Routes.Delete, method = RequestMethod.POST, consumes = MediaTypes.JSON)
	@ResponseBody
	public Map<String, String> delete(@PathVariable Long id) {
		Map<String, String> ajaxMap = getAjaxMap();
		mservice.deleteMyMessage(id,getCurrentUser().getId());
		return ajaxMap;
	}
}